//
//  UIButton+HWButtonCategory.m
//  嗨文
//
//  Created by 响当当 on 2019/1/15.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "UIButton+HWButtonCategory.h"

static void *MShitEdgeinsetKey = &MShitEdgeinsetKey;


@implementation UIButton (HWButtonCategory)
/**
 左边是文字，右边是图片（和原来的样式翻过来）
 
 @param space 间距
 */
- (void)setLeftTitleAndRightImageWithSpace:(CGFloat)space{
    
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
    
    // 测试的时候发现titleLabel的宽度不正确，这里进行判断处理
    CGFloat labelWidth = self.titleLabel.intrinsicContentSize.width;
    if (titleSize.width < labelWidth) {
        titleSize.width = labelWidth;
    }
    
    // 文字距左边框的距离减少imageView的宽度-间距，右侧增加距离imageView的宽度
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0.0, -imageSize.width - space, 0.0, imageSize.width)];
    
    // 图片距左边框的距离增加titleLable的宽度,距右边框的距离减少titleLable的宽度
    [self setImageEdgeInsets:UIEdgeInsetsMake(0.0, titleSize.width,0.0,-titleSize.width)];
}

/**
 上部分是图片，下部分是文字
 
 @param space 间距
 */
- (void)setUpImageAndDownLableWithSpace:(CGFloat)space{
    
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
    
    // 测试的时候发现titleLabel的宽度不正确，这里进行判断处理
    CGFloat labelWidth = self.titleLabel.intrinsicContentSize.width;
    if (titleSize.width < labelWidth) {
        titleSize.width = labelWidth;
    }
    
    // 文字距上边框的距离增加imageView的高度+间距，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
    [self setTitleEdgeInsets:UIEdgeInsetsMake(imageSize.height+space, -imageSize.width, -space, 0.0)];
    
    // 图片距右边框的距离减少图片的宽度，距离上面的间隔，其它不变
    [self setImageEdgeInsets:UIEdgeInsetsMake(-space-10, 0.0,0.0,-titleSize.width)];
}

/**
 设置角标的个数（右上角）
 
 @param badgeValue <#badgeValue description#>
 */
- (void)setBadgeValue:(NSInteger)badgeValue{
    
    CGFloat badgeW   = 20;
    CGSize imageSize = self.imageView.frame.size;
    CGFloat imageX   = self.imageView.frame.origin.x;
    CGFloat imageY   = self.imageView.frame.origin.y;
    
    UILabel *badgeLable = [[UILabel alloc]init];
    badgeLable.text = [NSString stringWithFormat:@"%ld",badgeValue];
    badgeLable.textAlignment = NSTextAlignmentCenter;
    badgeLable.textColor = [UIColor whiteColor];
    badgeLable.font = [UIFont systemFontOfSize:12];
    badgeLable.layer.cornerRadius = badgeW*0.5;
    badgeLable.clipsToBounds = YES;
    badgeLable.backgroundColor = [UIColor redColor];
    
    CGFloat badgeX = imageX + imageSize.width - badgeW*0.5;
    CGFloat badgeY = imageY - badgeW*0.25;
    badgeLable.frame = CGRectMake(badgeX, badgeY, badgeW, badgeW);
    [self addSubview:badgeLable];
}


#pragma mark - 初始化方法
/** 图片按钮初始化方法 */
+ (instancetype)imageButtonType:(UIButtonType)buttonType normalTitle:(NSString *)normalTitle normalImage:(UIImage *)normalImage highlightedImage:(UIImage *)highlightedImage {
    UIButton *button = [UIButton buttonWithType:buttonType];
    [button setTitle:normalTitle forState:UIControlStateNormal];
    [button setImage:normalImage forState:UIControlStateNormal];
    [button setImage:highlightedImage forState:UIControlStateHighlighted];
    return button;
}

/** 背景图片按钮初始化方法 */
+ (instancetype)imageButtonType:(UIButtonType)buttonType normalTitle:(NSString *)normalTitle normalBackImage:(UIImage *)normalBackImage highlightBackImage:(UIImage *)highlightBackImage {
    UIButton *button = [UIButton buttonWithType:buttonType];
    [button setTitle:normalTitle forState:UIControlStateNormal];
    [button setBackgroundImage:normalBackImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightBackImage forState:UIControlStateHighlighted];
    return button;
}

/** 文字按钮初始化方法 */
+ (instancetype)titleButtonType:(UIButtonType)buttonType normalTitle:(NSString *)normalTitle highlightTitle:(NSString *)highlightTitle normalBackImage:(UIImage *)normalBackImage highlightedBackImage:(UIImage *)highlightedBackImage {
    UIButton *button = [UIButton buttonWithType:buttonType];
    [button setTitle:normalTitle forState:UIControlStateNormal];
    [button setTitle:highlightTitle forState:UIControlStateHighlighted];
    [button setBackgroundImage:normalBackImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightedBackImage forState:UIControlStateHighlighted];
    return button;
}

#pragma mark - 文字
/** 普通文字 */
- (void)setNormalTitle:(NSString *)normalTitle {
    [self setTitle:normalTitle forState:UIControlStateNormal];
}
- (NSString *)normalTitle {
    if (!self.selected && !self.highlighted) {
        return self.titleLabel.text;
    }else {
        return nil;
    }
}
/** 高亮文字 */
- (void)setHighlightTitle:(NSString *)highlightTitle {
    [self setTitle:highlightTitle forState:UIControlStateHighlighted];
}
- (NSString *)highlightTitle {
    if (self.highlighted) {
        return self.titleLabel.text;
    }else {
        return nil;
    }
}
/** 选中文字 */
- (void)setSelectedTitle:(NSString *)selectedTitle {
    [self setTitle:selectedTitle forState:UIControlStateSelected];
}
- (NSString *)selectedTitle {
    if (self.selected) {
        return self.titleLabel.text;
    }else {
        return nil;
    }
}

#pragma mark - 文字颜色
/** 普通文字颜色 */
- (void)setNormalTitleColor:(UIColor *)normalTitleColor {
    [self setTitleColor:normalTitleColor forState:UIControlStateNormal];
}
- (UIColor *)normalTitleColor {
    if (!self.selected && !self.highlighted) {
        return self.titleLabel.textColor;
    }else {
        return nil;
    }
}

/** 高亮文字颜色 */
- (void)setHighlightTitleColor:(UIColor *)highlightTitleColor {
    [self setTitleColor:highlightTitleColor forState:UIControlStateHighlighted];
}
- (UIColor *)highlightTitleColor {
    if (self.highlighted) {
        return self.titleLabel.textColor;
    }else {
        return nil;
    }
}

/** 选中文字颜色 */
- (void)setSelectedTitleColor:(UIColor *)selectedTitleColor {
    [self setTitleColor:selectedTitleColor forState:UIControlStateSelected];
}
- (UIColor *)selectedTitleColor {
    if (self.selected) {
        return self.titleLabel.textColor;
    }else {
        return nil;
    }
}

#pragma mark - 图片
/** 普通图片 */
- (void)setNormalImage:(UIImage *)normalImage {
    [self setImage:normalImage forState:UIControlStateNormal];
}
- (UIImage *)normalImage {
    if (!self.selected && !self.highlighted) {
        return self.imageView.image;
    }else {
        return nil;
    }
}

/** 高亮图片 */
- (void)setHighlightImage:(UIImage *)highlightImage {
    [self setImage:highlightImage forState:UIControlStateHighlighted];
}
- (UIImage *)highlightImage {
    if (self.highlighted) {
        return self.imageView.image;
    }else {
        return nil;
    }
}

/** 选中图片 */
- (void)setSelectedImage:(UIImage *)selectedImage {
    [self setImage:selectedImage forState:UIControlStateSelected];
}
- (UIImage *)selectedImage {
    if (self.selected) {
        return self.imageView.image;
    }else {
        return nil;
    }
}

#pragma mark - 背景图片
/** 普通背景图片 */
- (void)setNormalBackImage:(UIImage *)normalBackImage {
    [self setBackgroundImage:normalBackImage forState:UIControlStateNormal];
}
- (UIImage *)normalBackImage {
    if (!self.selected && !self.highlighted) {
        return self.imageView.image;
    }else {
        return nil;
    }
}

/** 高亮背景图片 */
- (void)setHighlightBackImage:(UIImage *)highlightBackImage {
    [self setBackgroundImage:highlightBackImage forState:UIControlStateHighlighted];
}
- (UIImage *)highlightBackImage {
    if (self.highlighted) {
        return self.imageView.image;
    }else {
        return nil;
    }
}

/** 选中背景图片 */
- (void)setSelectedBackImage:(UIImage *)selectedBackImage {
    [self setBackgroundImage:selectedBackImage forState:UIControlStateSelected];
}
- (UIImage *)selectedBackImage {
    if (self.selected) {
        return self.imageView.image;
    }else {
        return nil;
    }
}

#pragma mark - 本项目常用按钮样式
// 圆角背景图片按钮
+ (instancetype)backImageBtnNormalTitle:(NSString *)normalTitle {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:normalTitle forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"nor_btn"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"hig_btn"] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[UIImage imageNamed:@"sel_btn"] forState:UIControlStateSelected];
    return button;
}
// 直角背景图片按钮
+ (instancetype)rightAngleImageBtnNormalTitle:(NSString *)normalTitle {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:normalTitle forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"angle_btn_nor"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"angle_btn_hig"] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[UIImage imageNamed:@"angle_btn_dis"] forState:UIControlStateSelected];
    return button;
}

@end
